﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Thuoc24h.BasePage;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;

namespace Thuoc24h.AdminCP.Pages
{
    public partial class NewsEdit : BasePages
    {
        private int _id;
        private News _obj = new News();
        private const int CategoryTypeId = 2;
        protected void Page_Load(object sender, EventArgs e)
        {

            _id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {
                radCreateDate.SelectedDate = DateTime.Now;
                cbIsDisplayOnList.Checked = true;
                FillDataListBoxRoot();
                NewsReferenes();
                if (_id > 0)
                {
                    btnSave.Text = ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = ActionNames.AddNew;
                }


            }

        }


        public void FillDataListBoxRoot()
        {
            radTreeCategories.DataSource = ReviewRepository.GetCategoryParent(CategoryTypeId);
            radTreeCategories.DataValueField = "Id";
            radTreeCategories.DataTextField = "Name";
            radTreeCategories.DataFieldParentID = "ParentId";
            radTreeCategories.DataFieldID = "Id";
            radTreeCategories.DataBind();

        }

        protected void radTreeCategories_NodeClick(object sender, RadTreeNodeEventArgs e)
        {


            radListbox.DataSource = ReviewRepository.GetNewsReferences(radTreeCategories.SelectedValue.ToInt32Return0());
            radListbox.DataBind();

        }


        private void GetData()
        {

            _obj.Title = txtName.Text;
            _obj.UnsignTitle = txtUnsignName.Text;
            _obj.CategoryId = radTreeCategories.SelectedValue.ToInt32Return0();
            _obj.Description = edtDescription.Text;
            _obj.Detail = edtDetail.Content;
            _obj.IsDisplayInOnFooter = cbIsDisplayInOnFooter.Checked;
            _obj.IsDisplayOnHeader = cbIsDisplayInOnHeader.Checked;
            _obj.IsDisplayOnList = cbIsDisplayOnList.Checked;
            _obj.IsDisplayOnSlider = cbIsDisplayInOnSlide.Checked;
            _obj.CreatedDate = radCreateDate.SelectedDate;
            _obj.ThumbImage = txtFileName.Text;
           
            if (_obj.CreatedBy != null)
            {
                _obj.CreatedBy = Websecurity.GetCurrentUser();
            }

        }
        private void Reset()
        {

            txtName.Text = string.Empty;
            txtUnsignName.Text = string.Empty;
            radTreeCategories.Nodes.Clear();
            edtDescription.Text = string.Empty;
            edtDetail.Content = string.Empty;
            cbIsDisplayOnList.Checked = true;
            cbIsDisplayInOnSlide.Checked = false;
            cbIsDisplayInOnHeader.Checked = false;
            cbIsDisplayInOnFooter.Checked = false;
            radCreateDate.SelectedDate = DateTime.Now;
            txtFileName.Text = string.Empty;

        }

        private void NewsReferenes()
        {
            var listReferences = ReviewRepository.GetNewsReferences();
            if (_id > 0)
            {
            listReferences= listReferences.Where(item => item.Id != _id).ToList();
            }
            radListbox.DataSource = listReferences;
            radListbox.DataBind();
        }
        private void SaveData()
        {
            if (_id > 0)
            {
                _obj = context.News.SingleOrDefault(item => item.Id == _id);
                if (_obj != null)
                {
                    ReviewRepository.RelatedNewsDelete_List(_id);
                    GetData();
                }

            }
            else
            {
                
                GetData();
                _obj.ViewCount = 0;
                context.News.AddObject(_obj);
            }

            if (context.SaveChanges() > 0)
            {
                if (_obj != null)
                {
                    var currentId = _obj.Id;

                    if (radListbox.Items.Count > 0)
                    {



                        IList<RadListBoxItem> collection = radListbox.CheckedItems;
                        foreach (RadListBoxItem item in collection)
                        {
                            var relatedNews = new RelatedNew();
                            relatedNews.NewsId = currentId;
                            int selectedItem = item.Value.ToInt32Return0();
                            relatedNews.RelatedNewsId = selectedItem;
                            context.RelatedNews.AddObject(relatedNews);
                            context.SaveChanges();
                        }


                    }
                }

                Response.Redirect(UrlManager.Admin.NewsView);


            }
        }
        private void BindData()
        {

            _obj = ReviewRepository.NewsById(_id);
            txtName.Text = _obj.Title;
            txtUnsignName.Text = _obj.UnsignTitle;
            edtDescription.Text = _obj.Description;
            edtDetail.Content = _obj.Detail;
            cbIsDisplayOnList.Checked = (bool) (_obj.IsDisplayOnList != null ? _obj.IsDisplayOnList : false);
            cbIsDisplayInOnSlide.Checked = (bool) (_obj.IsDisplayOnSlider != null ? _obj.IsDisplayOnSlider : false);
            cbIsDisplayInOnHeader.Checked = (bool) (_obj.IsDisplayOnHeader != null ? _obj.IsDisplayOnHeader : false);
            cbIsDisplayInOnFooter.Checked = (bool) (_obj.IsDisplayInOnFooter != null ? _obj.IsDisplayInOnFooter : false);
            radCreateDate.SelectedDate = _obj.CreatedDate;
            txtFileName.Text = _obj.ThumbImage;


            if (_obj.CategoryId != null)
            {
                var categoryId = _obj.CategoryId.Value;
                var node = radTreeCategories.FindNodeByValue(categoryId.ToString());
                node.Selected = true;
            }


            var listBoxs = context.RelatedNews.Where(item => item.NewsId == _id);
            foreach (var relatedNew in listBoxs)
            {
                //if (theItem == chkContactType.GetItemValue(i))
                //    chkContactType.SetItemChecked(i, true);

                radListbox.FindItemByValue(relatedNew.RelatedNewsId.ToString()).Checked = true;

            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();
        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }
    }
}